#!/bin/sh

### BEGIN INIT INFO
# Provides:        jail-shell
# Required-Start:  $network 
# Required-Stop:   $network 
# Default-Start:   2 3 4 5
# Default-Stop:
# Short-Description: Start jail-shell server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

JAIL_CMDD=/usr/sbin/jail-cmdd
PIDFILE=/var/run/jail-cmdd.pid

test -x $JAIL_CMDD || exit 5

case $1 in
	start)
		$JAIL_CMDD $JAIL_SHELL_OPTS
		while true; do
			if [ -e "$PIDFILE" ]; then
				break;
			fi
			sleep .5
		done
		PID="`cat $PIDFILE 2>/dev/null`"
		if [ -z "$PID" ]; then
			echo "start jail-shell server failed."
			exit 1
		fi
		if [ ! -e "/proc/$PID" ]; then
			echo "start jail-shell server failed."
			exit 1
		fi
		echo "start jail-shell server success."
		;;
	stop)
		if [ ! -f "$PIDFILE" ]; then
			echo "jail-shell server is stopped."
			exit 0
		fi
		PID="`cat $PIDFILE 2>/dev/null`"
		if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
			echo "jail-shell server is stopped"
			exit 0
		fi

		kill -TERM $PID
		if [ $? -ne 0 ]; then
			echo "Stop jail-shell server failed."
			exit 1;
		fi
		rm -f $PIDFILE
		echo "Stop jail-shell server success."
		;;
	restart)
		$0 stop && sleep 1 && $0 start
		;;
	status)
		PID="`cat $PIDFILE 2>/dev/null`"
		if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
			echo "jail-shell server is not running."
			exit 1
		fi
		echo "jail-shell server is running."
		status=$?
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 2
		;;
esac

exit $status

